/*************************************************************************
 * Copyright 2008 David Almilli
 *  
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at 
 *  
 *     http://www.apache.org/licenses/LICENSE-2.0 
 *      
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 * See the License for the specific language governing permissions and 
 * limitations under the License.
 **************************************************************************/
package com.tivo.hme.bananas;

import com.tivo.hme.sdk.Resource;

public class BSplitHighlights extends BHighlightsPlus {
	protected BHighlightsPlus leftHighlights;
	protected BHighlightsPlus rightHighlights;
	protected int rightWidth;
	protected int spacing;

	public BSplitHighlights(ISplitHighlightsLayout layout, int rightWidth) {
		this(layout, rightWidth, 0);
	}
	
	public BSplitHighlights(ISplitHighlightsLayout layout, int rightWidth, int spacing) {
		super(layout);
		leftHighlights = new BHighlightsPlus(createLeftLayout());
		rightHighlights = new BHighlightsPlus(createRightLayout());
		this.rightWidth = rightWidth;
		this.spacing = spacing;
	}

	@Override
    public void refresh(Resource animation) {
        synchronized (lock) {
        	super.refresh(animation);
        	leftHighlights.refresh(animation);
        	rightHighlights.refresh(animation);
        }
    }
	
	protected IHighlightsLayout createLeftLayout() {
		return new LeftLayout();
	}
	
	protected IHighlightsLayout createRightLayout() {
		return new RightLayout();
	}

	public BHighlightsPlus getLeftHighlights() {
		return leftHighlights;
	}

	public BHighlightsPlus getRightHighlights() {
		return rightHighlights;
	}

	protected class LeftLayout implements IHighlightsLayout {

		public BRect getHighlightBounds() {
			BRect rect = getLayout().getHighlightBounds();

            int rw = rect.width/2;
            if (rightWidth != -1) {
            	rw = rightWidth;
            }
            rect.x -= spacing;
            rect.width -= rw;
			return rect;
		}

		public boolean getHighlightIsVisible(int visible) {
			ISplitHighlightsLayout layout = (ISplitHighlightsLayout)getLayout();
			if (layout.getHighlightIsVisible(visible)) {
				return layout.getLeftHighlightIsVisible(visible);
			} else {
				return false;
			}
		}

		public BScreen getScreen() {
			return getLayout().getScreen();
		}
	}

	protected class RightLayout implements IHighlightsLayout {

		public BRect getHighlightBounds() {
			BRect rect = getLayout().getHighlightBounds();
            
            int rw = rect.width/2;
            if (rightWidth != -1) {
            	rw = rightWidth;
            }

            rect.x = rect.x+rect.width-rw;
            rect.width = rw;
			return rect;
		}

		public boolean getHighlightIsVisible(int visible) {
			ISplitHighlightsLayout layout = (ISplitHighlightsLayout)getLayout();
			if (layout.getHighlightIsVisible(visible)) {
				return layout.getRightHighlightIsVisible(visible);
			} else {
				return false;
			}
		}

		public BScreen getScreen() {
			return getLayout().getScreen();
		}
	}
	
	public static interface ISplitHighlightsLayout extends IHighlightsLayout {
		public boolean getLeftHighlightIsVisible(int visible);
		public boolean getRightHighlightIsVisible(int visible);
	}
}
